<template>
    <div id="box">
        <div id="search" style="padding-left: 150px">
            <el-form :inline="true" :model="searchForm" class="demo-form-inline">
                <el-form-item label="学生姓名">
                    <el-input v-model="searchForm.name" placeholder="学生姓名"></el-input>
                </el-form-item>
                <el-form-item label="班级">
                    <el-input v-model="searchForm.clazz" placeholder="班级"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit">查询</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div id="headerTool" style="padding-left: 150px;margin-bottom: 20px">
            <el-button type="primary" plain @click="addInfo">新增</el-button>
            <el-button type="danger" plain @click="deleteInfo">批量删除</el-button>
        </div>
        <!--   :data="tableData"    表格中需要渲染的数据
                label="日期"   表头
                width="180"   列宽
                prop="date"   当前列需要显示的数据
                @selection-change  点击复选框时间
         -->
        <el-table
            :data="tableData"
            @selection-change="handleSelectionChange"
            style="width: 80%;margin: 0 auto">
            <el-table-column
                type="selection"
                width="55">
            </el-table-column>

            <el-table-column
                prop="id"
                label="ID">
            </el-table-column>

            <el-table-column
                prop="studentNo"
                label="学号">
            </el-table-column>
            <el-table-column
                prop="name"
                label="姓名">
            </el-table-column>

            <el-table-column
                prop="age"
                label="年龄">
            </el-table-column>
            <el-table-column
                prop="clazz"
                label="班级">
            </el-table-column>
            <el-table-column
                prop="tel"
                label="联系方式">
            </el-table-column>
            <el-table-column
                prop="schoolYear"
                label="入学时间">
            </el-table-column>

            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        @click="handleEdit(scope.row)">编辑</el-button>
                    <el-button
                        size="mini"
                        type="danger"
                        @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>

        </el-table>
        <div class="block" style="padding-left: 150px;margin-top: 20px">
            <!--      current-page: 当前页   page-size: 每页展示的条数    total 数据总条数  page-sizes 下拉选选项 -->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="1"
                :page-sizes="[5, 15, 20, 25]"
                :page-size="5"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>

        <el-dialog title="新增" :visible.sync="dialogFormVisible" @close="clearInfo" >
            <el-form :model="addForm" ref="addForm" :label-position="labelPosition" label-width="80px" :rules="rules">
                <el-form-item label="学号" prop="studentNo" >
                    <el-input v-model="addForm.studentNo" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="姓名" prop="name">
                    <el-input v-model="addForm.name" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="年龄" prop="age">
                    <el-input v-model="addForm.age" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="班级" prop="clazz">
                    <el-input v-model="addForm.clazz" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="联系方式" prop="tel">
                    <el-input v-model="addForm.tel" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="入学时间" prop="schoolYear">
                    <el-date-picker type="date" placeholder="选择日期" v-model="addForm.schoolYear" style="width: 100%;"></el-date-picker>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="addStudent">确 定</el-button>
            </div>
        </el-dialog>
        <el-dialog title="编辑" :visible.sync="editDialogFormVisible" >
            <el-form :model="editForm" ref="editForm" :label-position="labelPosition" label-width="80px" :rules="rules">
                <el-form-item v-show="false" >
                    <el-input v-model="editForm.id" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="学号" prop="studentNo" >
                    <el-input v-model="editForm.studentNo" autocomplete="off" readonly></el-input>
                </el-form-item>

                <el-form-item label="姓名" prop="name">
                    <el-input v-model="editForm.name" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="年龄" prop="age">
                    <el-input v-model="editForm.age" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="班级" prop="clazz">
                    <el-input v-model="editForm.clazz" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="联系方式" prop="tel">
                    <el-input v-model="editForm.tel" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="入学时间" prop="schoolYear">
                    <el-date-picker type="date" placeholder="选择日期" v-model="editForm.schoolYear" style="width: 100%;"></el-date-picker>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="editDialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="editStudent">确 定</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
import instance from "@/http";

export default {
    name: "StudentView",
    data() {
        return {
            ids:[],
            labelPosition:'right',
            total:100,
            editDialogFormVisible:false,
            dialogFormVisible:false,
            editForm:{
                id:'',
                studentNo:'',
                name:'',
                age:'',
                clazz:'',
                tel:'',
                schoolYear:''
            },
            addForm:{
                studentNo:'',
                name:'',
                age:'',
                clazz:'',
                tel:'',
                schoolYear:''
            },
            searchForm:{
                name:'',
                clazz:'',
                page:1,
                limit:5
            },
            tableData: [],
            rules:{
                name: [
                    { required: true, message: '请输入学生姓名', trigger: 'blur' },
                ],
                age: [
                    { required: true, message: '请输入年龄', trigger: 'blur' },
                ],
                clazz: [
                    { required: true, message: '请输入班级', trigger: 'blur' },
                ],
                tel: [
                    { required: true, message: '请输入联系方式', trigger: 'blur' },
                ],
                schoolYear: [
                    { required: true, message: '请输入入学时间', trigger: 'blur' },
                ],
                studentNo: [
                    { required: true, message: '请输入学号', trigger: 'blur' },
                ]
            }
        }
    },
    methods:{
        // 清空表单检验规则
        clearInfo(){
            this.addForm = {
                studentNo:'',
                name:'',
                age:'',
                clazz:'',
                tel:'',
                schoolYear:''
            }
            this.$refs.addForm.clearValidate()
            this.$refs.editForm.clearValidate()
        },
        editStudent(){
            this.$refs.editForm.validate((valid) => {
                //  表单校验通过  true   否则 false
                if (valid) {
                    // 发送http请求
                    instance.post("/student/edit",this.editForm)
                        .then(res=>{
                            if (res.data.code==0){
                                this.$notify.success(res.data.message)
                                this.editDialogFormVisible = false
                            }else {
                                this.$notify.error(res.data.message)
                            }
                            this.studentPage()
                        })
                } else {
                    this.$notify.warning("请填写必要数据")
                }
            });
        },
        addStudent(){
            this.$refs.addForm.validate((valid) => {
                //  表单校验通过  true   否则 false
                if (valid) {
                    // 发送http请求
                    instance.post("/student/add",this.addForm)
                        .then(res=>{
                            if (res.data.code==0){
                                this.$notify.success(res.data.message)
                                this.dialogFormVisible = false
                            }else {
                                this.$notify.error(res.data.message)
                            }
                            this.studentPage()
                        })
                } else {
                    this.$notify.warning("请填写必要数据")
                }
            });
        },
        handleEdit(row){
            this.editDialogFormVisible = true
            // 将当前行数据回显在表单中
            this.editForm = row
        },
        handleDelete(row){

            this.$confirm('确认删除当前行数据?', '删除', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                // 发送http请求  this.ids = []
                instance.post("/student/batchDelete",[row.id])
                    .then(res=>{
                        if (res.data.code==0){
                            this.$notify.success(res.data.message)
                        }else {
                            this.$notify.error(res.data.message)
                        }
                        this.studentPage()
                    })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });


        },
        handleSelectionChange(val){
           this.ids =  val.map(row=>row.id)

        },
        //  改变每页显示数据条数时 会自动调用该函数  参数:当前页允许显示数据的条数
        handleSizeChange(limit){
            this.searchForm.limit=limit
            this.studentPage()
        },
        // 改变页码时会自动调用该函数    参数 :当前页的页码
        handleCurrentChange(currentPage){
            this.searchForm.page = currentPage
            this.studentPage()
        },
        onSubmit(){
            // 在搜索之前初始化页码为第一页  以免查询看不到数据
            this.searchForm.page = 1
            this.studentPage()
        },
        addInfo(){
            // 显示弹出层
            this.dialogFormVisible = true
        },
        deleteInfo(){

            // 判定用户是否已经选择的数据
            if (this.ids.length==0){
                this.$notify.warning("请至少选择一条数据")
                return false
            }

            instance.post("/student/batchDelete",this.ids)
                .then(res=>{
                    if (res.data.code==0){
                        this.$notify.success(res.data.message)
                    }else {
                        this.$notify.error(res.data.message)
                    }
                    this.studentPage()
                })
        },
        studentPage(){
            instance.post('/student/page',this.searchForm)
                .then(res=>{
                    if (res.data.code == 0) {
                        this.total = res.data.data.total
                        this.tableData = res.data.data.tableData
                    }
                })
        }
    },
    created() {
        this.studentPage()
    }
}
</script>

<style scoped>

</style>